package android.pot.com.smartcar.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by zhaoshilu on 2017/7/24.
 */
@SuppressLint("ValidFragment")
public class PayDialogView extends DialogFragment implements View.OnClickListener{

    private Context context;

    public PayDialogView(Context context) {
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View v = inflater.inflate(R.layout.dialog_pay_method, null);
        setCancelable(false);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialog_up_down;
        initBody(v);
        addListener();
        return v;
    }


    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = getDialog().getWindow()
                .getAttributes();
        params.width = dm.widthPixels*3/4;
        params.height = params.height;

        getDialog().getWindow().setAttributes(params);

        // 背景透明
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
    }

    private void initBody(View v) {
        v.findViewById(R.id.ll_wechat).setOnClickListener(this);
        v.findViewById(R.id.ll_alipay).setOnClickListener(this);

    }

    private void addListener(){
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_wechat:
                listener.methodClick(0);
                dismiss();
                break;
            case R.id.ll_alipay:
                listener.methodClick(1);
                dismiss();
                break;

        }
    }
    private OnMethodClickListener listener;

    public interface OnMethodClickListener {
        void methodClick(int method);
    }

    public void setMethodClickListener(OnMethodClickListener listener) {
        this.listener = listener;
    }
}

